package com.example.drive_safe;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;
import android.widget.ToggleButton;

public class MainActivity extends Activity {

	String ex_list=null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		final ToggleButton tb = (ToggleButton) findViewById(R.id.toggleButton1);
		tb.setOnClickListener(new OnClickListener() {
			// Application ON OFF button listener
			public void onClick(View v) {
				if (tb.getText().toString().equals("ON")) {
					startApp();
				} else {
					stopApp();
				}
				Toast.makeText(getBaseContext(),
						"Application is " + tb.getText().toString().toLowerCase() + "line", Toast.LENGTH_SHORT).show();
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	// select the items(About/Setting) from action bar
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		boolean result = true;
		switch (item.getItemId()) {
		case R.id.action_about:	
            displayAboutDialog();
			break;
		case R.id.action_settings:
            startActivity(new Intent(this, SettingsActivity.class));
			break;
		}
		return result;
	}
    
	// Start the application on the click upon OFF (on toggle button)
	public void startApp() {
		trigger("com.drivesafe.APPLICATION_ON");
	}

	// Stop the application on the click upon ON (on toggle button)
	public void stopApp() {
		trigger("com.drivesafe.APPLICATION_OFF");
	}
    
	// Change the status of the application between ON and OFF 
	public void trigger(String action) {
		Intent intent = new Intent();
		intent.setAction(action);
		sendBroadcast(intent);
	}

	// Show the About dialogue 
	private void displayAboutDialog() {
		AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);

		// set title
		alertDialogBuilder.setTitle("About");

		// set dialog message
		alertDialogBuilder.setMessage(R.string.about).setCancelable(true);

		// create alert dialog
		AlertDialog alertDialog = alertDialogBuilder.create();

		// show it
		alertDialog.show();
	}
}
